{% extends "helios/templates/cryptobase.html" %}

{% block content %}
<script>
function reset() {
    $('#processing').hide();
    $('#result').html("");
    $('#input').hide();
    $('#loading').show();
    BigInt.setup(function() {
        $('#loading').hide();
        $('#input').show();
    });
}

$(document).ready(function() {
    reset();
});

var PK_HASH = "{{trustee.public_key_hash}}";

function check_sk(sk_value) {
    $('#input').hide();
    $('#processing').show();

    try {
      var secret_key = ElGamal.SecretKey.fromJSONObject(jQuery.secureEvalJSON(sk_value));

      var pk_hash = b64_sha256(jQuery.toJSON(secret_key.pk));
      var key_ok_p = (pk_hash == PK_HASH);
    } catch (e) {
      debugger;
      var key_ok_p = false;
    }
    
    $('#processing').hide();

    var reset_link = "<br /><a href='javascript:reset();'>try again</a>";
    if (key_ok_p) {
      $('#result').html("your secret key matches!");
    } else {
      $('#result').html("OH OH, your key is bad." + reset_link);
    }
}
</script>
  <h2 class="title">{{election.name}} &mdash; Trustee {{trustee.name}} &mdash; Check Secret Key <span style="font-size:0.7em;">[<a href="{% url "election@trustee" election_uuid=election.uuid trustee_uuid=trustee.uuid %}">back to trustee home</a>]</span></h2>

<p>
Your public key fingerprint is: <b>{{trustee.public_key_hash}}</b>
</p>

<div id="loading">
loading crypto functions...
</div>

<div id="input" style="display:none;">
<p>
To verify that you have the right secret key, paste it here:
<p>

<form onsubmit="check_sk(this.secret_key.value); this.secret_key.value=''; return false;">
<textarea name="secret_key" cols="60" rows ="5" wrap="soft" style="height: 25em;">
</textarea>
<br />
<input type="submit" value="check" />
</form>
</div>

<div id="processing" style="display:none;">
checking your secret key...
</div>

<div id="result">
</div>


<div id="applet_div"></div>
{% endblock %}
